//
//  MessageCell.swift
//  chenChat
//
//  Created by chen on 16/2/20.
//  Copyright © 2016年 chen. All rights reserved.
//

import UIKit

let avatarSize: CGSize = CGSize(width: 45, height: 45)

class MessageCell: UITableViewCell {
    var avatar: UIImageView
    var messageLabel: UILabel
    var messageBackground: UIImageView
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        self.avatar = UIImageView()
        self.messageLabel = UILabel()
        self.messageBackground = UIImageView()
        self.messageLabel.font = UIFont.systemFontOfSize(16)
        //会自动返行
        self.messageLabel.numberOfLines = 0
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.addSubview(avatar)
        self.addSubview(messageBackground)
        self.addSubview(messageLabel)
        
        self.backgroundColor = UIColor.clearColor()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    class func reuseIdentifier() -> String {
        return "MessageCell"
    }
    class func heightForCell(message:Message) -> CGFloat {
        let screanRect = UIScreen.mainScreen().bounds
        let labelSize: CGSize = UILabel.sizeOfString(message.message, font: UIFont.systemFontOfSize(16), maxWidth: screanRect.width - 10 - 20 - avatarSize.width * 2)
        if labelSize.height < avatarSize.height + 10{
            return avatarSize.height + 10
        }else{
            return labelSize.height + 5
        }
    }
    func setupFriendCell(message: Message, user: UserInfo){
        //计算背景信息
        let screanRect = UIScreen.mainScreen().bounds
        let labelSize: CGSize = UILabel.sizeOfString(message.message, font: UIFont.systemFontOfSize(16), maxWidth: screanRect.width - 10 - 20 - avatarSize.width * 2)
        let messageBackgroundSize: CGSize = CGSizeMake(labelSize.width + 20, labelSize.height + 5)
        //true则是发送者，false则是被发送者
        if message.incoming{
            self.avatar.frame = CGRectMake(screanRect.width - avatarSize.width - 5, 5, avatarSize.width, avatarSize.height)
            self.avatar.image = UIImage(named: "icon01.jpg")
            
            self.messageBackground.image = UIImage.resizableImage("chatto_bg_normal.png")
            self.messageBackground.frame = CGRectMake(screanRect.width - 5 - avatarSize.width - messageBackgroundSize.width, 5, messageBackgroundSize.width, messageBackgroundSize.height)
            
            self.messageLabel.text = message.message
            self.messageLabel.frame = CGRectMake(screanRect.width - avatarSize.width - messageBackgroundSize.width , 5, labelSize.width, labelSize.height)
            self.messageLabel.textAlignment = .Center
            
        } else{
            self.avatar.frame = CGRectMake(5, 5, avatarSize.width, avatarSize.height)
            self.avatar.image = UIImage(named: "icon02.jpg")
            
            self.messageBackground.image = UIImage.resizableImage("chatfrom_bg_normal.png")
            self.messageBackground.frame = CGRectMake(5 + avatarSize.width + 5, 5, messageBackgroundSize.width, messageBackgroundSize.height)
            self.messageLabel.text = message.message
            self.messageLabel.frame = CGRectMake( avatar.frame.width + 5 + 15 + 10, 5, labelSize.width, labelSize.height)
            self.messageLabel.textAlignment = .Left
        }
        
    }

}
